# from collections import defaultdict
#
# lis1 = [{'age': 18, 'name': '冯亚奇'}, {'age': 18, 'name': '李勇泽'},
#         {'age': 21, 'name': '王晓丽'}, {'age': 21, 'name': '牛嘉恒'}]
#
# b = defaultdict(list)
# for i in lis1:
#     b[i['age']].append(i['name'])
#
# print(dict(b))


# str = 'How to perform a left join in SQLALchemx?'
#
# print(' '.join(str.split(' ')[::-1]).replace('?','') + '?')


# 去重set
# a =["某云教育","某云教育"]
# b = set(a)
# print(b)
#
# # 交集
# l1 = list(set(a) & set(b))
# print(l1)
#
# # 去重
# l2 = list(set(a) | set(b))
# print(l2)
#
# # 只存在新闻1里 不存在新闻2里的字
# l3 = list(set(a) - set(b))
# print(l3)
#
# # 只存在新闻2里 不存在新闻1里的字
# l4 = list(set(b) - set(a))
# print

# class Singleton(object):
#     def __new__(cls, *args, **kwargs):
#         if not hasattr(cls, "_instance"):   # hasatter() 判断一个对象是否有某个属性
#             cls._instance = super().__new__(cls, *args, **kwargs)
#         return cls._instance
#
# print(Singleton() is Singleton())   # True


class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):   # hasatter() 判断一个对象是否有某个属性
            cls._instance = super().__new__(cls)
        return cls._instance

    # __new__方法无法避免触发__init__()，初始的成员变量会进行覆盖
    def __init__(self, name=None):
        self.name = name

    def print_name(self):
        print(self.name)

a1, a2 = Singleton('a1'), Singleton('a2')
a1.print_name()
a2.print_name()

